package com.haoze.nurseapp.controller;

import com.haoze.common.response.Result;
import com.haoze.common.response.ResultGenerator;
import com.haoze.nurseapp.service.PatientService;
import com.haoze.nurseapp.service.feign.EmrService;
import com.haoze.nurseapp.service.feign.NurseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/nurseapp/patient")
@Api(value = "护理表单目录")
@Validated
public class PatientController {

	@Resource
	PatientService patientService;


	@Resource
    EmrService emrService;

    @GetMapping("/patientInfoCards")
	@ApiOperation(value = "获取指定病区病人信息卡")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query", example = "1"),
			@ApiImplicitParam(name = "pageSize", value = "页长", dataType = "Integer", paramType = "query", example = "20"),
			@ApiImplicitParam(name = "wardId", value = "病区ID", dataType = "String", paramType = "query", example = "1CD50685DD29416EB58F2E9D9DAC013E")
	})
	public Result getNursePatients(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam String wardId,
								   @RequestParam(required = false) String search) {
		return patientService.getPatientInfoCards(page, pageSize, wardId,search);
	}

	@GetMapping("/patientInfo")
	@ApiOperation(value = "获取病人信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "qrCode", value = "二维码编码", dataType = "String", paramType = "query", example = "20200319000050008", required = true)
	})
	public Result getPatientInfo(@RequestParam(required = true) String qrCode) {
		return patientService.getPatientInfo(qrCode);
	}

	@GetMapping("/patientPeopleInfo")
    @ApiOperation(value = "医嘱信息，获取病人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNo", value = "住院号", dataType = "String", paramType = "query", example = "20200319000050008", required = true)
    })
    public Result patientPeopleInfo(@RequestParam String inpatNo) {
        return emrService.detail(inpatNo);
    }

	@GetMapping("/patientPeopleInfoFromNurse")
    @ApiOperation(value = "医嘱信息，获取病人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNo", value = "住院号", dataType = "String", paramType = "query", example = "20200319000050008", required = true)
    })
    public Result patientPeopleInfoFromNurse(@RequestParam String inpatNo) {
        return patientService.getPatientPeopleDetail(inpatNo);
    }

/*	@GetMapping("/patientPeopleInfoFromHis")
    @ApiOperation(value = "医嘱信息，获取病人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNo", value = "住院号", dataType = "String", paramType = "query", example = "20200319000050008", required = true)
    })
    public Result patientPeopleInfoFromHis(@RequestParam String inpatNo) {
        return patientService.getPatientPeopleDetailFromHis(inpatNo);
    }*/

    @GetMapping("/conditionInfo")
    @ApiOperation(value = "病情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "query", example = "1097434-1", required = true),
			@ApiImplicitParam(name = "wardId", value = "病区", dataType = "String", paramType = "wardId", example = "1097434-1", required = false)
    })
    public Result conditionInfo(@RequestParam(required = true) String inpatNum,@RequestParam(required = false) String wardId) {
    	if("undefined".equals(inpatNum)){
    		return ResultGenerator.genOkResult();
		}else{
			return patientService.conditionInfo(inpatNum,wardId);
		}
    }

    @GetMapping("/depts")
	@ApiOperation(value = "获取授权科室或病区")
	public Result getDepts() {
		return patientService.getDepts();
	}


	@GetMapping("/exam")
	@ApiOperation(value = "获取检查列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "inpatNum", value = "住院号", required = true),
			@ApiImplicitParam(name = "page", value = "页码",required = true),
			@ApiImplicitParam(name = "pageSize", value = "页长",required = true),
			@ApiImplicitParam(name = "type", value = "检查状态(空是全部，1，报告已出，2 报告未出)"),
			@ApiImplicitParam(name = "times", value = "检查时间")
	})
	public Result queryExam(
			@RequestParam final String inpatNum,
			@RequestParam Integer page,
			@RequestParam Integer pageSize,
			@RequestParam(required = false) final String type,
			@RequestParam(required = false) final String times) {
		return ResultGenerator.genOkResult(patientService.getExamList(inpatNum, times,type,page,pageSize));
	}


	@GetMapping("/examDetail")
	@ApiOperation(value = "获取检查详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "examItemId", value = "exam_item主键",required = true)
	})
	public Result getExamDetail(@RequestParam String examItemId,
								@RequestParam(required = false) String reqNum,
								@RequestParam(required = false) String inpatNum){
		/*if(!StringUtils.isEmpty(examItemId) && !"".equals(examItemId)){
			return ResultGenerator.genOkResult(patientService.getExamDetail(examItemId,reqNum));
		}else{
			//return ResultGenerator.genFailedResult("无检查数据!");
			return ResultGenerator.genOkResult(patientService.getExamDetail(examItemId,reqNum));
		}*/
		if(!StringUtils.isEmpty(examItemId) && "null".equals(examItemId)){
			examItemId=null;
		}
		return ResultGenerator.genOkResult(patientService.getExamDetail(examItemId,reqNum,inpatNum));

	}



	@GetMapping("/lab")
	@ApiOperation(value = "获取检验列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "inpatNum", value = "住院号", required = true),
			@ApiImplicitParam(name = "times", value = "时间"),
			@ApiImplicitParam(name = "page", value = "页码",required = true),
			@ApiImplicitParam(name = "pageSize", value = "页长",required = true),
			@ApiImplicitParam(name = "type", value = "检验状态(空是全部，1，已出报告，2未出报告)"),
	})
	public Result getLab(@RequestParam String inpatNum,
						 @RequestParam Integer page,
						 @RequestParam Integer pageSize,
						 @RequestParam(required = false) String type,
						 @RequestParam(required = false) String times) {
		if (StringUtils.hasLength(times)) {
			times += " 23:59:59";
		}
		return ResultGenerator.genOkResult(patientService.getLabList(inpatNum, type, times,page,pageSize));
	}

	@GetMapping("/labDetail")
	@ApiOperation(value = "获取检验详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "labItemDataId", value = "PATIENT_LAB_ITEM 主键",required = true)
	})
	public Result getLabDetail(@RequestParam String labItemDataId) {
		return  ResultGenerator.genOkResult(patientService.geLabDetail(labItemDataId));
	}


	/**
	 * 检查预约查询
	 */
	@GetMapping("/queryExamAppointment")
	@ApiOperation(value = "检查预约查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "dateTime", value = "预约时间", required = false),
			@ApiImplicitParam(name = "type", value = "0未预约 1已预约 2已检查", required = false),
			@ApiImplicitParam(name = "wardDeptId", value = "病区", required = false),
			@ApiImplicitParam(name = "checkDeptId", value = "检查部门", required = false),
			@ApiImplicitParam(name = "keyWords", value = "模糊查询", required = false),
			@ApiImplicitParam(name = "page", value = "页码",required = false),
			@ApiImplicitParam(name = "pageSize", value = "页长",required = false),
	})
	public Result queryExamAppointment(
			@RequestParam(required = false) String dateTime,
			@RequestParam(required = false) String type,
			@RequestParam(required = false) String  wardDeptId,
			@RequestParam(required = false) String checkDept,
			@RequestParam(required = false) String keyWords,
			@RequestParam(required = false) Integer page,
			@RequestParam(required = false) Integer pageSize) {
		return patientService.queryExamAppointment(dateTime, type,wardDeptId,checkDept,keyWords,page,pageSize);
	}

	/**
	 * 检查预约查询
	 */
	@GetMapping("/queryCheckDept")
	@ApiOperation(value = "查询检查部门")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "dateTime", value = "预约时间", required = false),
			@ApiImplicitParam(name = "type", value = "0未预约 1已预约 2已检查", required = false),
			@ApiImplicitParam(name = "wardDeptId", value = "病区", required = false),
			@ApiImplicitParam(name = "checkDeptId", value = "检查部门", required = false),
			@ApiImplicitParam(name = "keyWords", value = "模糊查询", required = false)
	})
	public Result queryCheckDept(
			@RequestParam(required = false) String dateTime,
			@RequestParam(required = false) String type,
			@RequestParam(required = false) String  wardDeptId,
			@RequestParam(required = false) String checkDept,
			@RequestParam(required = false) String keyWords) {
		return patientService.queryCheckDept(dateTime,type,wardDeptId,checkDept,keyWords);
	}


	/**
	 * 检查预约查询打印查询
	 */
	@GetMapping("/queryExamAppointmentPrint")
	public Result queryExamAppointmentPrint(
			@RequestParam(required = false) String dateTime,
			@RequestParam(required = false) String type,
			@RequestParam(required = false) String  wardDeptId,
			@RequestParam(required = false) String checkDept,
			@RequestParam(required = false) String keyWords,
			@RequestParam(required = false) Integer page,
			@RequestParam(required = false) Integer pageSize,
			@RequestParam(required = false) String patexamreportdataIds) {
		return patientService.queryExamAppointmentPrint(dateTime, type,wardDeptId,checkDept,keyWords,patexamreportdataIds,page,pageSize);
	}

	@GetMapping("/queryDailyReport")
	@ApiOperation(value = "出入院日报查询")
	public Result queryDailyReport(@RequestParam String deptCode,
								   @RequestParam String startDatetime,
								   @RequestParam String endDatetime) {
		return patientService.queryDailyReport(deptCode, startDatetime, endDatetime);
	}





}
